﻿using dreamwork.mobile.app.api.Core;
using dreamwork.mobile.app.api.Infrastructure;
using dreamwork.mobile.app.api.Models.Data;
using dreamwork.mobile.app.api.Models.ViewModel;
using DereamWork.Model.ViewModel;
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using System.Threading.Tasks;

namespace dreamwork.mobile.app.api.Services
{
    public class NormalUserService : INormalUserService
    {
        private readonly INormalUserRepository _normalUserRepository;
        private readonly ITokenService _tokenService;
        public NormalUserService(INormalUserRepository normalUserRepository, ITokenService tokenService)
        {
            _normalUserRepository = normalUserRepository;
            _tokenService = tokenService;
        }
        public async Task<UserLoginResult> LoginAsync(NormalUser user)
        {
            var result = new UserLoginResult() {
                UserType = UserTypes.NormalUser
            };
            if (user == null)
            {
                result.LoginResult = false;
                result.Message = "提交信息不完整";
                return result;
            }
            var model = await _normalUserRepository.GetAsync(x => x.PhoneNumber == user.PhoneNumber);
            if (model == null && !string.IsNullOrEmpty(user.Name))
            {
                model = await _normalUserRepository.GetAsync(x => x.Name == user.Name.ToLower());
            }
            if (model == null)
            {
                result.LoginResult = false;
                result.Message = "用户不存在";
                return result;
            }
            if (model.Password != user.Password)
            {
                result.LoginResult = false;
                result.Message = "密码不正确";
                return result;
            }
            result.LoginResult = true;
            result.Message = "登录成功";
            result.Token = new UserToken
            {
                Value = _tokenService.CreateToken(model.Id)
            };
            return result;
        }
        public async Task<UserLogoutResult> LogoutAsync()
        {
            var result = new UserLogoutResult()
            {
                UserType = UserTypes.NormalUser,
                LogoutResult= true
            };
            return await Task.FromResult(result);
        }
        public async Task<ServiceResult<NormalUser>> GetByIdAsync(string id)
        {
            var model = await _normalUserRepository.GetAsync(id);
            var result = new ServiceResult<NormalUser>();
            if (model == null)
            {
                result.Message = "信息不存在";
                result.IsSuccess = false;
                return result;
            }
            result.Data = model;
            return result;
        }
        public async Task<ServiceResult<NormalUser>> ModifyAsync(NormalUser user)
        {
            if (user == null)
            {
                throw new System.ArgumentNullException(nameof(user));
            }

            var model = await _normalUserRepository.GetAsync(user.Id);
            var result = new ServiceResult<NormalUser>();
            if (model == null)
            {
                result.Message = "要修改的用户不存在";
                result.IsSuccess = false;
                return result;
            }
            await _normalUserRepository.UpdateAsync(user);
            result.Data = await _normalUserRepository.GetAsync(user.Id);
            return result;
        }

    }
}